﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Win32;

namespace AxUtilUI
{
    public enum AxVersion
    {
        Ax2012 = 1,
        Ax2012R2 = 2
    }

    public class ClientDetails
    {
        public string Version { get; set; }
        public string Location { get; set; }

        public static ClientDetails GetClientDetailsFromRegistry(string servername)
        {
            ClientDetails details = new ClientDetails();
            Microsoft.Win32.RegistryKey componentKey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, servername).OpenSubKey(@"SOFTWARE\Microsoft\Dynamics\6.0\Setup");
            if (componentKey != null)
            {
                string axComponent32Location = componentKey.GetValue("InstallDir32") as string;
                if (!string.IsNullOrEmpty(axComponent32Location))
                {
                    string axClientLocation = string.Format(@"{0}\Client\Bin\Ax32.exe", axComponent32Location);
                    if (System.IO.File.Exists(axClientLocation))
                    {
                        System.Diagnostics.FileVersionInfo versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(axClientLocation);
                        details.Version = versionInfo.ProductVersion;
                    }
                    details.Location = axClientLocation;
                }
            }
            if (details.Location == string.Empty)
            {
                details.Location = "Client not found.";
            }

            return details;
        }
    }

    public class AosDetails
    {
        public string AosServer { get; set; }
        public string AosInstance { get; set; }
        public string AosPath { get; set; }
        public string AosSqlServer { get; set; }
        public string AosSqlDatabase { get; set; }
        public string AosSqlModelDatabase { get; set; }
        public AxVersion AosVersion { get; set; }
        public string ActiveConfig { get; set; }
        public string ProductVersion { get; set; }

        public static List<AosDetails> GetAosDetailsFromRegistry(string servername)
        {
            List<AosDetails> aosDetails = new List<AosDetails>();
            Microsoft.Win32.RegistryKey mainKey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, servername).OpenSubKey(@"SYSTEM\CurrentControlSet\Services\Dynamics Server\6.0");
            if (mainKey != null)
            {
                foreach (string aos in mainKey.GetSubKeyNames())
                {
                    AosDetails aosDetail = new AosDetails();
                    Microsoft.Win32.RegistryKey aosKey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, servername).OpenSubKey(string.Format(@"SYSTEM\CurrentControlSet\Services\Dynamics Server\6.0\{0}", aos));
                    string config = aosKey.GetValue("Current") as string;
                    aosDetail.ActiveConfig = config;
                    string aosPath = aosKey.GetValue("AOSPath") as string;
                    aosDetail.AosPath = aosPath;
                    string aosProdVersion = aosKey.GetValue("ProductVersion") as string;
                    if (!string.IsNullOrEmpty(aosProdVersion))
                    {
                        aosProdVersion = aosProdVersion.Substring(0, 3);
                        aosDetail.AosVersion = aosProdVersion == "6.2" ? AxVersion.Ax2012R2 : AxVersion.Ax2012;
                    }
                    string aosVersion = string.Empty;
                    if (!string.IsNullOrEmpty(aosPath) && System.IO.File.Exists(aosPath))
                    {
                        System.Diagnostics.FileVersionInfo versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(aosPath);
                        aosVersion = versionInfo.ProductVersion;
                        aosDetail.ProductVersion = aosVersion;
                    }
                    aosDetail.AosInstance = aosKey.GetValue("InstanceName") as string;
                    if (!string.IsNullOrEmpty(config))
                    {
                        Microsoft.Win32.RegistryKey aosConfigKey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, servername).OpenSubKey(string.Format(@"SYSTEM\CurrentControlSet\Services\Dynamics Server\6.0\{0}\{1}", aos, config));
                        string aosSqlServer = aosConfigKey.GetValue("dbserver") as string;
                        aosDetail.AosSqlServer = aosSqlServer;
                        string aosDatabase = aosConfigKey.GetValue("database") as string;
                        aosDetail.AosSqlDatabase = aosDatabase;
                        string aosModelDatabase = string.Empty;
                        if (aosProdVersion == "6.2" && aosConfigKey.GetValue("ModelDBServer_Baseline") != null)
                        {
                            aosModelDatabase = aosDatabase + "_Model";
                            aosDetail.AosSqlModelDatabase = aosModelDatabase;
                        }
                    }
                    aosDetails.Add(aosDetail);
                }
                Microsoft.Win32.Registry.LocalMachine.Close();
            }

            return aosDetails;
        }
    }

    public class AosServer
    {
        public string ServiceName { get; set; }
        public string ServerName { get; set; }
        public int ServerId { get; set; }
        public bool EnableBatch { get; set; }
        public int ServerSessions { get; set; }
        public int ClientSessions { get; set; }
        public int WebSessions { get; set; }
    }
}
